﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using App1AppHandler;
using Reborn_WorldServer;
using Reborn_WorldServer.App1AppHandler;

namespace Reborn_Server.App1AppHandler
{
    public class Potion_AllRegen : Buff, IConsumable
    {
        public Potion_AllRegen()
        {
            minutesLeft = 10;
        }

        public bool MeetsConsumeRequirements(Character character, Loot loot)
        {
            return true;
        }

        public void Consume(Character myCharacter, Loot lootitem)
        {
            TargetCreatureBase = myCharacter;

            LockItem lockItem = TargetCreatureBase.GameHandler.GetLockItem(TargetCreatureBase.MapID, TargetCreatureBase.Instance);
            lock (lockItem.LockObj)
            {
                Map CurrentMap = TargetCreatureBase.GameHandler.GetMap(TargetCreatureBase.MapID, TargetCreatureBase.Instance);

                List<Character> CharactersWithin4 = new List<Character>();
                foreach (MapItem mapitem in CurrentMap.MapItems)
                {
                    if (Math.Abs(mapitem.MapX - myCharacter.MapX) <= 8 && Math.Abs(mapitem.MapY - myCharacter.MapY) <= 8)
                    {
                        foreach (CreatureBase creaturebase in mapitem.CreaturesBases)
                        {
                            if (creaturebase is Character)
                            {
                                Character character = (Character)creaturebase;
                                character.PlaySound("DrinkBottle", myCharacter.MapX, myCharacter.MapY);
                            }
                        }
                    }
                    if (Math.Abs(mapitem.MapX - myCharacter.MapX) <= 4 && Math.Abs(mapitem.MapY - myCharacter.MapY) <= 4)
                    {
                        foreach (CreatureBase creaturebase in mapitem.CreaturesBases)
                        {
                            if (creaturebase is Character)
                                CharactersWithin4.Add((Character)creaturebase);
                        }
                    }
                }

                SpellIcon potion = new SpellIcon();
                this.SpellID = TargetCreatureBase.GameHandler.ItemIDGen.GetSpellID();
                potion.ClassName = lootitem.ClassName;
                potion.Description = lootitem.Description;
                potion.ImageURL = lootitem.ImageURL;
                potion.Name = lootitem.Name;
                potion.Offensive = false;
                potion.CastType = "Self";

                this.spellIcon = potion;
                this.SpellID = Managers.GameHandler.ItemIDGen.GetSpellID();

                //Remove existing heal potions
                foreach (Buff buff in TargetCreatureBase.buffManager.Buffs)
                {
                    if (buff.spellIcon.Name == base.spellIcon.Name)
                    {
                        buff.RemoveSpell(false, false);
                    }
                }

                TargetCreatureBase.HPRegen = TargetCreatureBase.HPRegen + 1;
                TargetCreatureBase.StaminaRegen = TargetCreatureBase.StaminaRegen + 1;
                TargetCreatureBase.ManaRegen = TargetCreatureBase.ManaRegen + 1;
                
                TargetCreatureBase.buffManager.AddBuff(this);
                TargetCreatureBase.buffManager.UpdateBuffs();

                foreach (Character character in CharactersWithin4)
                    character.AddCreatureBase(TargetCreatureBase);
            }
            spellTimer.Start();
        }
        public override void RemoveSpell(bool updateBuffs, bool sendMessage)
        {
            base.RemoveSpell(updateBuffs, sendMessage);

            TargetCreatureBase.HPRegen = TargetCreatureBase.HPRegen - 1;
            TargetCreatureBase.StaminaRegen = TargetCreatureBase.StaminaRegen - 1;
            TargetCreatureBase.ManaRegen = TargetCreatureBase.ManaRegen - 1;
        }
    }
}
